<template>
  <div class="container">
    <div class="top">
      <h3 class="title"><span />模板文件</h3>
      <el-button size="small" type="primary" icon="el-icon-arrow-left" class="back" @click="prev">返回</el-button>
    </div>
    <el-table
      :data="themeData"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
      style="margin-top:36px"
    >
      <el-table-column label="ID" prop="id" width="80px" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="操作名" prop="action" />
      <el-table-column label="文件路径" prop="file">
        <template slot-scope="scope">
          {{ scope.row.file }}.html
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="description" />
      <el-table-column fixed="right">
        <template slot-scope="scope">
          <el-button class="edit" size="mini" icon="el-icon-edit" @click="goEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <BouncedDialog id="file" title="编辑文件" :dialog-show.sync="dialogShow" :width="80">
      <div class="btn">
        <el-button type="primary" class="save" @click="submit()">保存</el-button>
        <!-- <el-button size="mini" @click="pre">上一个</el-button>
        <el-button size="mini" @click="next">下一个</el-button> -->
      </div>
      <Editor
        ref="editor"
        v-model="tempForm.content"
        height="500"
        width="100%"
        :options="{
          enableBasicAutocompletion: true,
          enableSnippets: true,
          enableLiveAutocompletion: true,
          tabSize:6,
          fontSize:14,
          showPrintMargin:false, //去除编辑器里的竖线
          tabSize:2,//换行符大小
        }"
        :lang="'html'"
        theme="monokai"
        @init="editorInit"
      />

    </BouncedDialog>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import mouldApi from '@/api/theme'
import Editor from 'vue2-ace-editor'
export default {
  components: {
    Editor
  },
  data() {
    return {
      website_id: '',
      theme_id: '',
      theme: '',
      themeData: [],
      dialogShow: false,
      tempForm: {
        content: '',
        path: ''
      },
      tempRules: {
        content: [
          { required: 'true', message: '内容不能为空', tigger: 'blur' }
        ]
      },
      lang: '',
      themeName: '',
      arr: [// brace/theme文件夹下的所有主题名字
        'ambiance',
        'chaos',
        'chrome',
        'clouds',
        'clouds_midnight',
        'cobalt',
        'crimson_editor',
        'dawn',
        'dracula',
        'dreamweaver',
        'eclipse',
        'github',
        'gob',
        'gruvbox',
        'idle_fingers',
        'iplastic',
        'katzenmilch',
        'kr_theme',
        'kuroir',
        'merbivore',
        'merbivore_soft',
        'monokai',
        'mono_industrial',
        'pastel_on_dark',
        'solarized_dark',
        'solarized_light',
        'sqlserver',
        'terminal',
        'textmate',
        'tomorrow',
        'tomorrow_night',
        'tomorrow_night_blue',
        'tomorrow_night_bright',
        'tomorrow_night_eighties',
        'twilight',
        'vibrant_ink',
        'xcode'
      ],
      num: 0,
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      } // 分页器
    }
  },
  created() {
    this.website_id = this.$route.query.id
    this.theme = this.$route.query.tname
    this.theme_id = this.$route.query.tid
    this.lang = this.$route.query.lang

    this.getList()
  },
  methods: {
    editorInit() {
      require('brace/theme/monokai')
      // for (let i = 0;i < this.arr.length;i++) {
      //   require('brace/theme/' + this.arr[i])
      // }
      require('brace/mode/html')
      require('brace/mode/json')
      require('brace/snippets/json')
      require('brace/snippets/html')
      require('brace/snippets/css')
    },
    pre() {
      if (this.num == 0) {
        return
      }
      this.num--
      this.themeName = this.arr[this.num]
    },
    next() {
      if (this.num == this.arr.length - 1) {
        return
      }
      this.num++
      this.themeName = this.arr[this.num]
    },
    getList() {
      const val = {
        website_id: this.website_id,
        theme: this.theme,
        theme_id: this.theme_id,
        page: this.page.currentPage,
        limit: this.page.size,
        lang: this.lang
      }
      mouldApi.tmpFile(val).then(res => {
        this.themeData = res.data
        this.page.total = res.count
      })
    },
    goEdit(row) {
      this.tempForm.path = this.website_id + '/' + this.lang + '/' + this.theme + '/' + row.file + '.html'
      mouldApi.fileRead({ path: this.tempForm.path }).then(res => {
        if (res.code == 0) {
          this.tempForm.content = res.data
          //
          this.dialogShow = true
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    submit() {
      mouldApi.fileUpdate({ ...this.tempForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.dialogShow = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    },
    prev() {
      this.$router.push({ name: 'Theme' })
    }
  }
}
</script>
<style lang='scss'>
#file{
  .el-dialog{
    background-color: #282722;
  }
  .el-dialog__header{
    padding: 10px 20px 1px;
    .el-dialog__title{
      color: #fff;
      font-size: 16px;
      // font-weight: bold;
    }
  }
  .el-dialog__body{
    padding: 0;
  }
  .save.el-button--primary{
    width: 54px;
    height: 25px;
    border-color: #565656;
    background-color: #565656;
    font-size: 12px;
  }
  .btn{
    margin-top: 0;
    margin-bottom: 10px;
    margin-left: 15px;
    text-align: left;
  }
}
</style>
<style scoped lang='scss'>
.top{
    display: flex;
    justify-content: left;
    align-items: center;
    .back{
        top: 16px;
        right: 21px;
    }
}
.el-table{
    margin-bottom: 20px;
}

</style>
